#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>

int main(int argc,char **argv)
{
    int r_c;
    int w_c;
    struct stat file;
    stat(argv[1],&file);
    int file_size =file.st_size;
    printf("%d\n",file_size);
    if(argc != 3)
    {
        printf("please enter two file");
        exit(1);
    }
    int fdin = open(argv[1],O_RDONLY); 
    if(fdin == -1)
    {
        printf("%s open error",argv[1]);
        exit(1);
    }
    
    int fdout= open(argv[2],O_WRONLY | O_CREAT,0655);
    if(fdout == -1)
    {
        printf("%s open error",argv[2]);
        exit(1);
    }
    char buf[1024]={0};
    while((r_c=read(fdin,buf,sizeof(buf))))
    {      
        if((r_c==-1)&&(errno != EINTR))
        {
            break;
        }

        if((w_c = write(fdout,buf,r_c)) < 0)
        {
            perror("write data error");
            exit(1);
        }

        file_size = file_size - w_c;
        printf("%d\n",file_size);
        if(file_size <= 0)
        {
            break;
        }
        memset(buf,0,sizeof(buf));
    }
    close(fdin);
    close(fdout);

    return 0;
}